package com.yager.controller;

import com.yager.model.User;
import com.yager.service.CollectionService;
import com.yager.service.HouseService;
import com.yager.service.OrderService;
import com.yager.service.UserService;
import com.yager.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HouseService houseService;


//    @ResponseBody
    @PostMapping("/register")
    public String RegisterNewUser(@RequestParam("userImg") MultipartFile file, HttpServletRequest req){
        String name=req.getParameter("name");
        String mobile=req.getParameter("mobile");
        String email=req.getParameter("email");
        String pwd=req.getParameter("pwd");
        System.out.println(mobile+"   "+pwd);
        int flag=userService.registerNewUser(file,name,mobile,email,pwd);
        if(flag==1) return "user/loginPage";
        else if(flag==0) return "user/registerPage_ERROR_mobile";
        else return "user/registerPage_ERROR_username";

    }

//    @ResponseBody
    @RequestMapping("/login")
    public String login(HttpServletRequest req,HttpSession session){
        String mobile=req.getParameter("mobile");
        String pwd=req.getParameter("pwd");

        boolean judge=userService.login(mobile,pwd);
        System.out.println("进行了一次登录请求");
        if(judge){
            User user=userService.getUserByMobileOrNameOrEmail(mobile);

//            HttpSession session=req.getSession();
            session.setAttribute("user",user);
            System.out.println("登陆成功，走到了这里");
            return "house/homePage";
        }
        else {
            System.out.println("登陆失败，走到了这里");
            return "redirect:loginPage_ERROR.html";
        }
    }

//    @ResponseBody
    @PostMapping("/changeInfo")
    public String changeInfo(@RequestParam("userImg") MultipartFile file,HttpServletRequest req,HttpSession session){
        String name=req.getParameter("user_name");
        String gender=req.getParameter("user_gender");
        String job=req.getParameter("user_job");
        String code=req.getParameter("user_code");
        String realName=req.getParameter("user_realname");
        String mobile=req.getParameter("user_tel");
        String email=req.getParameter("user_email");
        String signature=req.getParameter("user_signature");

        User user=(User) session.getAttribute("user");
        user.setUsername(name);
        user.setUsergender(gender);
        user.setUserjob(job);
        user.setUsercode(code);
        user.setUserrealname(realName);
        user.setUsersignature(signature);
        user.setUseremail(email);
        user.setUsertel(mobile);

        System.out.println(user);
        userService.updateUserInfo(user,file);
        session.setAttribute("user",user);

        return "user/userInfo";
    }

    @RequestMapping("/findPwd1")
    public String findPwd1(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String mobile=req.getParameter("mobile");
        User user=userService.getUserByMobile(mobile);
        if(user!=null){
            EmailUtil emailUtil=new EmailUtil();
            String confirm=emailUtil.getAndSendConfirmation(user.getUseremail());
//            System.out.println("findPwd1方法中获取的confirm为："+confirm);

            HttpSession session=req.getSession();
            session.setAttribute("confirmation",confirm);
            session.setAttribute("findPwdUserId",user.getUserid());

//            req.setAttribute("confirmation",confirm);
//            resp.sendRedirect("FindBackPwd2.html");
//            req.getRequestDispatcher("FindBackPwd2.html").forward(req,resp);
            return "user/FindBackPwd2";
        }
        else{
            return "user/FindBackPwd1_ERROR.html";
        }
    }

    @RequestMapping("/findPwd2")
    public String findPwd2(HttpServletRequest req,HttpServletResponse resp) throws  Exception{
        HttpSession session=req.getSession();
        String realConfirm=(String) session.getAttribute("confirmation");
        String confirmation=req.getParameter("confirmation");

        System.out.println("真实验证码："+realConfirm+"   收到的验证码："+confirmation);


        if(realConfirm.equals(confirmation)){
            session.removeAttribute("confirmation");
            return "user/FindBackPwd3.html";
        }
        else{
            return "user/FindBackPwd2_ERROR.html";
        }
    }

    @RequestMapping("/findPwd3")
    public String findPwd3(HttpServletRequest req,HttpServletResponse resp){
        String newpwd=req.getParameter("pwd");
        HttpSession session=req.getSession();
        String userid=(String) session.getAttribute("findPwdUserId");
        session.removeAttribute("findPwdUserId");

        userService.changeUserPwd(userid,newpwd);
        session.removeAttribute("user");
        return "user/loginPage";
    }

    @RequestMapping("/toUserInfo")
    public String toUserInfo(){
        System.out.println("lets go to userinfo!!");
        return "user/userInfo";
    }

    @RequestMapping("/toUserInfo_change")
    public String toUserInfo_change(){
        return "user/userInfo_change";
    }

    @RequestMapping("/changePwd")
    public String changePwd(HttpSession session){
        User user=(User) session.getAttribute("user");
        session.setAttribute("findPwdUserId",user.getUserid());
        return "user/FindBackPwd3";
    }

    @RequestMapping("/killUser")
    public String killUser(HttpSession session){
        User user=(User) session.getAttribute("user");
        userService.changeUserStatusById(user.getUserid());
        userService.killUser(user.getUserid());

        session.invalidate();

        return "overall/Cover";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();

        return "overall/Cover";
    }


    @RequestMapping("/test")
    public String test1(){
        return "user/registerPage";
    }


}
